package service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import common.dao.BookMapper;
import common.dao.OrderMapper;
import common.pojo.Book;
import common.pojo.Order;
import common.utils.Constants;

@Service
public class BookServiceImpl {

	@Autowired
	private BookMapper mapper;
	@Autowired
	private OrderMapper omapper;

	/**
	 * 查询数量
	 * 
	 * @param bType
	 * @return
	 */
	public int queryByCount(Book b) {
		return mapper.queryByCount(b);
	}

	/**
	 * 查询列表
	 * 
	 * @param start
	 * @param limit
	 * @return
	 */
	public List<Book> find(int start, int limit) {
		return mapper
				.queryListByWhere(" b join book_type bt on b.type_id=bt.id where b.deleted="
						+ Constants.NOT_DELETED
						+ " order by b.create_time desc limit "
						+ start
						+ ","
						+ limit);
	}

	/**
	 * 增加
	 * 
	 * @param book
	 * @param dprice
	 * @param price
	 */
	public void add(Book book, String price, String dprice) {
		book.setState(Constants.SHOW);
		book.setDeleted(Constants.NOT_DELETED);
		book.setPrice((long) (Double.parseDouble(price) * 100));
		book.setDiscountPrice((long) (Double.parseDouble(dprice) * 100));
		mapper.add(book);
	}

	/**
	 * 查询
	 */
	public Book findInfoById(String id) {
		return mapper.queryById(Integer.parseInt(id));
	}

	/**
	 * 更新
	 * 
	 * @param b
	 */
	public void update(Book book, String price, String dprice) {
		book.setPrice((long) (Double.parseDouble(price) * 100));
		book.setDiscountPrice((long) (Double.parseDouble(dprice) * 100));
		mapper.updateBySelective(book);
	}

	/**
	 * 删除
	 * 
	 * @param ids
	 * @param adminName
	 */
	public void delete(String ids, String adminName) {
		String[] id = StringUtils.split(ids, ",");
		Book book = new Book();
		for (int i = 0; i < id.length; i++) {
			if (StringUtils.isNotBlank(id[i])) {
				book.setDeleted(Constants.DELETED);
				book.setUpdateName(adminName);
				book.setUpdateTime(new Date());
				book.setId(Integer.valueOf(id[i]));
				mapper.updateBySelective(book);
			}
		}
	}

	/**
	 * 统计
	 * 
	 * @param start
	 * @param limit
	 * @param endDate
	 * @param startDate
	 * @return
	 */
	public List<Book> getCount(int start, int limit, String startDate,
			String endDate) {
		List<Book> list = mapper.getCount(start, limit);
		for (int i = 0, length = list.size(); i < length; i++) {
			int all = omapper
					.queryCountByWhere(" o join order_detail od on o.id=od.order_id where od.book_id="
							+ list.get(i).getId());
			list.get(i).setAll(all);
			int cancel = omapper.queryCountByWhere(" o join order_detail od on o.id=od.order_id where od.book_id="
					+ list.get(i).getId()+" and o.state=0");
			list.get(i).setCancel(cancel);

			list.get(i).setDeal(all - cancel);
			list.get(i).setPrice((long) 1);
		}
		return list;
	}

}
